<?php
class BasicHelper {

	/**
	 * GET方式执行页面
	 *
	 * @param unknown $url        	
	 * @param unknown $postArr        	
	 * @return unknown
	 */
	function __https_request($url) {
		$curl = curl_init ();
		curl_setopt ( $curl, CURLOPT_URL, $url );
		curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
		curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );
// 		curl_setopt ( $curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
		curl_setopt ( $curl, CURLOPT_SSLVERSION, 1);
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
		$data = curl_exec ( $curl );
// 		$res = curl_getinfo($curl);
// 		echo "<pre>";
// 		print_r($res);
		if (curl_errno ( $curl )) {
			return 'ERROR ' . curl_error ( $curl );
		}
		curl_close ( $curl );
		return $data;
	}

	/**
	 * POST方式执行页面
	 *
	 * @param unknown $url        	
	 * @param unknown $postArr        	
	 * @return unknown
	 */
	function __http_post_request($url, $postArr) {
		$curl = curl_init ();
		curl_setopt ( $curl, CURLOPT_URL, $url );
		
		curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false );
// 		curl_setopt ( $curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
		curl_setopt ( $curl, CURLOPT_SSLVERSION, 1);
		curl_setopt ( $curl, CURLOPT_TIMEOUT, 60 );
		curl_setopt ( $curl, CURLOPT_CONNECTTIMEOUT, 15 );
		
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 0 );
		curl_setopt ( $curl, CURLOPT_POST, 1 );
		curl_setopt ( $curl, CURLOPT_POSTFIELDS, $postArr );
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
		$data = curl_exec ( $curl );
// 		$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
// 		print_r($httpCode);die;
// 		$res = curl_getinfo($curl);
// 		echo "<pre>";
// 		print_r($res);
		curl_close ( $curl );
		return $data;
	}
	/**
	 * 计算2个时间戳之间相差的天时分秒
	 * @param 开始时间 $begin_time
	 * @param 结束时间 $end_time
	 * @return multitype:number unknown
	 */
	function __timediff($begin_time, $end_time) {
		if ($begin_time < $end_time) {
			$starttime = $begin_time;
			$endtime = $end_time;
		} else {
			$starttime = $end_time;
			$endtime = $begin_time;
		}
		$timediff = $endtime - $starttime;
		$days = intval ( $timediff / 86400 );
		$remain = $timediff % 86400;
		$hours = intval ( $remain / 3600 );
		$remain = $remain % 3600;
		$mins = intval ( $remain / 60 );
		$secs = $remain % 60;
		$res = array (
				"day" => $days,
				"hour" => $hours,
				"min" => $mins,
				"sec" => $secs
		);
		return $res;
	}
        /**
	 * 最简单的XML转数组
	 * @param string $xmlstring XML字符串
	 * @return array XML数组
	 */
	public static function simplest_xml_to_array($xmlstring)
	{
		$array = simplexml_load_string($xmlstring,'SimpleXMLElement',LIBXML_NOCDATA);
		$newArray = array ( ) ;
		$array = ( array ) $array ;
		foreach ( $array as $key => $value )
		{
			$value = ( array ) $value ;
			$newArray [ $key] = $value [ 0 ] ;
		}
		$newArray = array_map("trim", $newArray);
		return $newArray ;
	}
	
	/**
	 * 多层级的xml转换为数组
	 */
	public static function xml_json_array($xmlfile){
		$ob= simplexml_load_string($xmlfile);
		$json  = json_encode($ob);
		$configData = json_decode($json, true);
		return $configData;
	}
}
